package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.entity.Dish;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    /**
     * 购物车添加商品
     * @param shoppingCartDTO
     */
    @Transactional
    @Override
    public void  add(ShoppingCartDTO shoppingCartDTO){
        // 1 加入购物车的商品是否在购物车存在
        // 1-1 根据T套餐Id/菜品Id 与用户Id的 查询
        Long uID = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, uID);
        queryWrapper.eq(
                shoppingCartDTO.getSetmealId()!=null,
                ShoppingCart::getSetmealId,
                shoppingCartDTO.getSetmealId()
                );
        queryWrapper.eq(
                shoppingCartDTO.getDishId()!=null,
                ShoppingCart::getDishId,
                shoppingCartDTO.getDishId()
        );
        queryWrapper.eq(
                StringUtils.isNotEmpty(shoppingCartDTO.getDishFlavor()),
                ShoppingCart::getDishFlavor,
                shoppingCartDTO.getDishFlavor()
        );
        ShoppingCart cart = super.getOne(queryWrapper);
        if (cart!=null){
            // 2 存在 count+1
            cart.setNumber(cart.getNumber()+1);
            super.updateById(cart);
            return;
        }
        // 3 不寻找 创建
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 补充数据 name  amount  image
        shoppingCart.setUserId(uID); // userId
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        if (shoppingCartDTO.getDishId()!=null){
            // 是菜品
            DishVO dishVO = dishService.getById(shoppingCartDTO.getDishId());
            shoppingCart.setName(dishVO.getName());
            shoppingCart.setAmount(dishVO.getPrice());
            shoppingCart.setImage(dishVO.getImage());
        }else {
            // 是套餐
            SetmealVO setmealVO = setmealService.getById(shoppingCartDTO.getSetmealId());
            shoppingCart.setName(setmealVO.getName());
            shoppingCart.setAmount(setmealVO.getPrice());
            shoppingCart.setImage(shoppingCart.getImage());
        }
        // 保存
        super.save(shoppingCart);
    }

    /**
     * 购物车删减商品
     * @param shoppingCartDTO
     */
    @Transactional
    @Override
    public void  sub(ShoppingCartDTO shoppingCartDTO){
        // 1 删除购物车的商品是否在购物车存在
        // 1-1 根据T套餐Id/菜品Id 与用户Id的 查询
        Long uID = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, uID);
        queryWrapper.eq(
                shoppingCartDTO.getSetmealId()!=null,
                ShoppingCart::getSetmealId,
                shoppingCartDTO.getSetmealId()
        );
        queryWrapper.eq(
                shoppingCartDTO.getDishId()!=null,
                ShoppingCart::getDishId,
                shoppingCartDTO.getDishId()
        );
        queryWrapper.eq(
                StringUtils.isNotEmpty(shoppingCartDTO.getDishFlavor()),
                ShoppingCart::getDishFlavor,
                shoppingCartDTO.getDishFlavor()
        );
        ShoppingCart cart = super.getOne(queryWrapper);
        if (cart == null) return;;
        if (cart.getNumber() <= 1){
            // 删除改购物车数据
            super.removeById(cart.getId());
        }else {
            // 数量减1
            cart.setNumber(cart.getNumber()-1);
            super.updateById(cart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart(){
        // 获取当前用户Ud
        Long uId = BaseContext.getCurrentId();
        // 查询
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, uId);
        List<ShoppingCart> shoppingCartList = super.list(queryWrapper);
        return  shoppingCartList;
    }

    /**
     * 清空购物车
     */
    @Override
    public void  cleanShoppingCart(){
        // 查询用户Id
        Long uId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, uId);
        super.remove(queryWrapper);
    }
}
